<template>
  <div class="permission-container">
    <div class="app-container">
      <el-card>
        <el-row type="flex" justify="end" style="margin-bottom: 30px">
          <el-button type="primary" size="small" @click="addRights(1, '0')">添加权限</el-button>
        </el-row>
        <!-- 表格
          1. 表格数据包含children属性
          2. 必须指定row-key="id"
        -->
        <el-table border stripe :data="rightsList" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <!-- {{ row }} -->
              <!-- <el-button v-if="row.pid === '0'">添加权限点</el-button> -->
              <el-button v-if="row.type === 1" @click="addRights(2, row.id)">添加权限点</el-button>
              <el-button @click="showEditRights(row.id)">查看</el-button>
              <el-button @click="delRights(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 添加/修改权限的对话框 -->
      <!-- 新增编辑的弹层 -->
      <!-- 新增编辑的弹层 -->
      <el-dialog :visible="showDialog" title="弹层标题" @close="closeDialog">
        <!-- 表单内容 -->
        <el-form label-width="100px">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="handleSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { reqGetPermissionList, reqAddPermission, reqGetPermissionDetail, reqDelPermission, reqUpdatePermission } from '@/api/permission'
import { tranListToTreeData } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    return {
      rightsList: [],
      showDialog: false,
      formData: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    }
  },
  created() {
    this.getAllRights()
  },
  methods: {
    // 获取所有权限点
    async getAllRights() {
      const { data } = await reqGetPermissionList()
      this.rightsList = tranListToTreeData(data, '0')
    },
    // 添加权限
    addRights(type, pid) {
      this.showDialog = true
      // console.log(type, pid)
      this.formData.type = type
      this.formData.pid = pid
    },
    // 关闭对话框
    closeDialog() {
      this.showDialog = false
      this.formData = {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    },
    // 确定提交
    async handleSubmit() {
      // 1. 预校验
      if (this.formData.id) {
        // 编辑
        await reqUpdatePermission(this.formData)
      } else {
        // 新增
        // 2. 发送新增的请求
        await reqAddPermission(this.formData)
        // 3. 提示
      }
      // 4. 关闭对话框
      this.closeDialog()
      // 5. 重新渲染
      this.getAllRights()
    },
    // 点击编辑展示对话框
    async showEditRights(id) {
      // 1. 查询点击的权限详情
      const { data } = await reqGetPermissionDetail(id)
      // 2. 回显数据
      this.formData = data
      // 3. 展示对话框
      this.showDialog = true
    },
    // 删除权限
    delRights(id) {
      this.$confirm('确定要删除该权限吗?', '温馨提示').then(async() => {
        await reqDelPermission(id)
        this.getAllRights()
        this.$message.success('删除成功')
      }).catch(() => {
        console.log('取消')
      })
    }
  }
}
</script>

<style>

</style>
